package com.flrjcx.service.impl;

import com.flrjcx.common.constants.DateConstants;
import com.flrjcx.common.constants.HttpStateConstants;
import com.flrjcx.common.constants.MessageConstants;
import com.flrjcx.common.model.UserModel;
import com.flrjcx.common.utils.*;
import com.flrjcx.dao.RegisterDao;
import com.flrjcx.common.enums.ValidStatusEnum;
import com.flrjcx.common.model.result.ResponseData;
import com.flrjcx.service.RegisterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author Flrjcx
 */
@Service
@Transactional
@Slf4j
public class RegisterServiceImpl implements RegisterService {
    @Resource
    private RegisterDao registerDao;
    @Autowired
    private EmailSendUtils emailSendUtils;
    @Resource
    private RedisCache redisCache;


    /**
     * 用户注册的所有信息校验通过后
     */
    @Override
    public ResponseData verifyRegisterBeforeAll(UserModel userModel) {
        String passName;
        String passEmail;
        // 如果redis掉了,怎么办?
        if (redisCache.verifyKey(userModel.getName())){
                passName = redisCache.getKey(userModel.getName());
                passEmail = redisCache.getKey(userModel.getEmail());
                System.out.println("redis");
        }else {
            passName = registerDao.selectRegisterUsersName(userModel.getName());
            passEmail = registerDao.selectRegisterUsersEmail(userModel.getEmail());
        }
        String verifyRegisterMsg = VerifyNameUtils.verifyRegisterMsg(userModel.getName(), passName, userModel.getPassword(), userModel.getEmail(), passEmail, userModel.getAddress(), userModel.getPhone());
        if (!MessageConstants.VERIFY_NAME_RESULT_MSG.equals(verifyRegisterMsg)){
            return ResponseData.buildErrorResponse(HttpStateConstants.HTTP_STATE_402,verifyRegisterMsg);
        }
        /** 校验通过后的md5*/
        String passPassword = EncryptionUtils.md5(userModel.getPassword());
        /** 校验通过后的uuid */
        String userId = EncryptionUtils.UUID(userModel.getName());

        /** 验证邮箱
        Integer validateCode = ValidateCodeUtils.generateValidateCode(6);
        jedisPool.getResource().setex(email+RedisMessageConstant.REGISTER_CODE,300, String.valueOf(validateCode));
        emailSendUtils.sendFixedMail(email,validateCode,name);
         邮箱验证搬走,不在这里发送验证
         */

        userModel.setAge(DateUtils.stampToStrDate(userModel.getAge(), DateConstants.TIME_FORMAT_YMD));

        //注册用户
        registerDao.insertUsers(userModel, userId);
        redisCache.registerSaveRedis(userModel.getName(),passPassword,userModel.getEmail());
        if (userModel.getSex().equals(ValidStatusEnum.ValidStatus.getCode())) {
            return ResponseData.buildResponse( MessageConstants.REGISTER_PASS + "欢迎" + userModel.getName() + "小姿力男士！",HttpStateConstants.HTTP_STATE_200);
        } else {
            return ResponseData.buildResponse(userModel.getName() + MessageConstants.REGISTER_PASS + "，欢迎" + userModel.getName() + "小姿力女士！",HttpStateConstants.HTTP_STATE_200);
        }
    }
}
